<?php
require_once('../smarty_admin.php');

$catalog_id = $_GET['catalog_id'];
//var_dump($catalog_id);
if (isset($_POST['submit']))
{	
	$error = '';
	$message = '';
	$submit = trim($_POST['submit']);
	$title = trim($_POST['title']);
	$sabo = trim($_POST['sabo']);
	$content = trim($_POST['content']);
	$ngay = $_POST['Date_Day'];
	$thang = $_POST['Date_Month'];
	$nam = $_POST['Date_Year'];
	$datetime = $thang.'-'.$ngay.'-'.$nam;
	$name = trim($_POST['name']);
	$img = trim($_POST['img']);
	$active = trim($_POST['active']);
	
	if ( ! strlen($title) > 0) {
		
		$error .= '<p>Ban chua nhap titile!</p>';
	}
	if ( ! strlen($sabo) > 0) {
		
		$error .= '<p>Ban chua nhap sabo!</p>';
	}
	if ( ! strlen($content) > 0) {
		
		$error .= '<p>Ban chua nhap content!</p>';
	}
	if ($error == '')
	{
		$message = '';
		if(isset($_FILES["img"]) && @$_FILES["img"]["name"] != "")
		{	
			
			
			if (! strlen($_POST['name']) >0) {
				$message .= '<p>Ban chua nhap ten cho hinh anh!</p>';
			}
			if ($message == '')
			{
				$name_truth_up = $_FILES['img']['name'];
				$name_truth = date('YmdHis');
				$file = BASE. '/data/upload/'.$name_truth.'.jpg';
				move_uploaded_file($_FILES["img"]["tmp_name"],$file)/*or die ('chua upload duoc file')*/;
				
				$sql = "INSERT INTO `articles` ( `article_id` , `catalog_id` , `title` , `sabo` , `content` , `datetime` , `img_name_alias` , `img_name_truth` , `active` ) VALUES (NULL , '".$catalog_id."', '".$title."', '".$sabo."', '".$content."', '".$datetime."', '".$name."', '".$name_truth."', '".$active."')";
				
					
				var_dump($sql);
				$result = mysql_query($sql);
				var_dump($result);
			}
			else 
			{
				echo $message;
			}
			
		}
		else 
		{
			if (! strlen($_POST['img']) > 0) 
			{
				$message .= 'Ban chua chon file hinh de upload!';
			}
			
			if ($message == '')
			{
				$sql = "INSERT INTO `articles` ( `article_id` , `catalog_id` , `title` , `sabo` , `content` , `datetime` , `img_name_alias` , `img_name_truth` , `active` ) VALUES (NULL , '".$catalog_id."', '".$title."', '".$sabo."', '".$content."', '".$datetime."', 'NULL', 'NULL', '".$active."')";
				var_dump($sql);
				$result = mysql_query($sql);
				var_dump($result);
			}
			
			else 
			{
				echo $message;
			}
		}
		
		
		
	
	}
	else 
	{
		echo $error;
		
	}
}	




$smarty->display('article/add.tpl');
?>